package service;

import DAO.LogDaoImpl;
import beans.Logs;
import beans.User;
import utils.DateUtil;

import java.util.List;

public class LogService {
    /*
     * 插入日志
     */
    public static void addLogs(User user,String logContext){
        LogDaoImpl logdaoimpl = new LogDaoImpl();
        Logs log = new Logs();
        log.setUserName(user.getName());
        log.setTimes(DateUtil.getDataTimeNow());
        log.setLogContext(logContext);
        logdaoimpl.addLog(log);
    }

    /*
     * 查询日志
     */
    public static String queryLogs(User user) {
        LogDaoImpl logdaoimpl = new LogDaoImpl();
        List<Logs> logsList = logdaoimpl.queryLog(user.getId());
        StringBuilder sb = new StringBuilder();
        sb.append("该账号的日志记录如下：\n");
        sb.append("账 号\t时 间\t操 作\n");
        for (Logs log : logsList) {
            sb.append(log.getUserName()).append("\t").append(log.getTimes())
                    .append("\t").append(log.getLogContext()).append("\n");
        }
        return sb.toString();
    }


}
